{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sklearn import svm\n",
    "import scipy.io\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "import random"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "pathname = 'hw01_data/'\n",
    "mnist = scipy.io.loadmat(pathname + 'mnist/train_new.mat')\n",
    "mnist_hog = scipy.io.loadmat(pathname + 'mnist/train_hog66.mat')\n",
    "test = scipy.io.loadmat(pathname + 'mnist/test.mat')\n",
    "test_hog = scipy.io.loadmat(pathname + 'mnist/test_hog66.mat')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "trainX = np.concatenate((mnist_hog['trainX'][:, :-1], mnist['trainX']), axis=1)\n",
    "validationX = np.concatenate((mnist_hog['validationX'][:, :-1], mnist['validationX']), axis=1)\n",
    "allX = np.concatenate((trainX, validationX), axis=0)\n",
    "training_data = allX[:, :-1]\n",
    "training_labels = allX[:, -1:].ravel()\n",
    "test_data = np.concatenate((test_hog['testX'], test['testX']), axis=1)\n",
    "C = 40 # to be tuned"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "test prediction saved\n"
     ]
    }
   ],
   "source": [
    "clf = svm.LinearSVC(C=C)\n",
    "clf.fit(training_data, training_labels)\n",
    "test_predict = clf.predict(test_data)\n",
    "\n",
    "# save predict output\n",
    "f = open('mnist_kaggle.csv', 'w')\n",
    "f.write('Id,Category\\n')\n",
    "for i in range(0, len(test_data)):\n",
    "\ts = str(i)+','+str(test_predict[i])+'\\n'\n",
    "\tf.write(s)\n",
    "f.close()\n",
    "print('test prediction saved')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [conda env:dsb2017]",
   "language": "python",
   "name": "conda-env-dsb2017-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
